package com.xy.view.recyclerview;

import android.databinding.ViewDataBinding;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * RecyclerView有头部的通用适配器
 * Created by Ybao on 16/7/22.
 */
public class HeaderStandardAdapter extends StandardAdapter {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private List<Object> mHeaderViews;
    StandardAdapter standardAdapter;

    public HeaderStandardAdapter(StandardAdapter standardAdapter) {
        this.standardAdapter = standardAdapter;
        mHeaderViews = new ArrayList<>();
    }

    public HeaderStandardAdapter addHeaderView(View headerView) {
        if (headerView != null) {
            mHeaderViews.add(headerView);
            notifyItemInserted(0);
        }
        return this;
    }

    public HeaderStandardAdapter addHeaderView(ViewDataBinding headerView) {
        if (headerView != null) {
            mHeaderViews.add(headerView);
            notifyItemInserted(0);
        }
        return this;
    }

    @Override
    public int getItemIndex(Object object) {
        int index = standardAdapter.getItemIndex(object);
        if (index >= 0) {
            return index + mHeaderViews.size(); //每一个的index要加上头部的size才是真正的index
        }
        return -1;
    }

    public List<Object> getHeaderViews() {
        return mHeaderViews;
    }

    @Override
    public void setData(List<?> mList) {
        standardAdapter.setData(mList);
        notifyDataSetChanged();
    }

    @Override
    public List<?> getData() {
        return standardAdapter.getData();
    }

    public void remove(int i) {
        if (standardAdapter != null) {
            List mList = standardAdapter.getData();
            if (mList != null && mList.size() > i + 1) {
                mList.remove(i);
                notifyItemChanged(i + mHeaderViews.size());
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public int getViewType(int position) {
        if (position < mHeaderViews.size()) {
            return TYPE_HEADER;
        }
        return TYPE_NORMAL;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        int viewType = getViewType(position);
        ItemViewHolder holder;
        if (viewType != TYPE_NORMAL) {
            Object obj = mHeaderViews.get(position);
            if (obj instanceof View) {
                holder = new ItemViewHolder((View) obj, false);
            } else {
                holder = new ItemViewHolder((ViewDataBinding) obj, false);
            }
        } else {
            holder = standardAdapter.onCreateViewHolder(parent, standardAdapter.getItemViewType(position - mHeaderViews.size()));
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        if (getViewType(position) != TYPE_NORMAL) {
            return;
        }
        standardAdapter.onBindViewHolder(holder, position - mHeaderViews.size());
        holder.setItemClickListener(thisOnItemClickListener);
    }

    @Override
    public int getItemCount() {
        return standardAdapter.getItemCount() + mHeaderViews.size();
    }

    @Override
    public Object getItem(int arg0) {
        return standardAdapter.getItem(arg0 - mHeaderViews.size());
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = (GridLayoutManager) manager;
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) { //getSpanSize 可以确定每一个item的大小
                    //需要确定头部的宽度，加入是gridLayout的话，要占满一行
                    return getViewType(position) != TYPE_NORMAL ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

    @Override
    public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        super.unregisterAdapterDataObserver(observer);
        if (standardAdapter != null) {
            standardAdapter.unregisterAdapterDataObserver(observer);
        }
    }

    @Override
    public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        super.registerAdapterDataObserver(observer);
        if (standardAdapter != null) {
            standardAdapter.registerAdapterDataObserver(observer);
        }
    }
}
